<?php
import("ORG.Util.Page");
import("@.ORG.Url");
class MoveAction extends Action {
    //数据库班级动态回复类型.
    public static $CLASS_MOVE = 1;

    //班级动态
    public function index()
    {
        //左侧最新留言
        $result = commonModel::getLeaveData();
        //动态
        $move = M("move");
        $listcount = $move->count();
        $Page = new Page($listcount, PAGE_NUMBER);
        $show = $Page->show();
        $list = $move->limit($Page->firstRow.','.$Page->listRows)->order("m_date  desc")->select();
        // echo $move->getLastSql();  exit();
        // 角色
        $isAdmin = UserModel::hasRules(UserModel::ADMIN_USER);

        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

        // 验证删除权限
        $isHasDelete = UserModel::isRoles(UserModel::USER_DELETE);

        $this->assign('isHasDelete', $isHasDelete);
        $this->assign('isHasInsert', $isHasInsert);
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign("result", $result);
        $this->display();
    }

    public function Mcontent()
    {
        //获取文章的详细信息.
        $list = M("move")->join("tp_user on tp_user.u_id = tp_move.u_id")->where("tp_move.m_id='{$this->_get('mid')}'")->field("tp_move.*,tp_user.u_name")->limit('1')->find();
        
        // 获取动态信息
        $move = new MoveModel();
        $moveInfo = $move->getMoveOne($this->_get('mid'));

        // 获取用户信息
        $userInfo = M("user1")->where("u_id={$moveInfo['u_id']}")->find();

        //回复.
        $replay_zb = M("replay_zb");

        //获得留言的条数
        $listcount = $replay_zb->table("tp_user u,tp_replay_zb r")->where("zb_type = '" . self::$CLASS_MOVE . "' and Link_id='{$this->_get('mid')}' and r.u_id=u.u_id ")->count();

        //分页条数
        $Page = new Page($listcount, PAGE_NUMBER);
        $show = $Page->show();
        //分页内容

        $listzb = $replay_zb->table("tp_user u,tp_replay_zb r")->where("zb_type = '" . self::$CLASS_MOVE . "' and Link_id='{$this->_get('mid')}' and r.u_id=u.u_id ")->order("r.zb_date desc ")->limit($Page->firstRow.','.$Page->listRows)->select();

        //上一个
        $lastRecord = MoveModel::lastSectionRecord($this->_get('mid'));
        //下一个
        $nextRecord = MoveModel::nextSectionRecord($this->_get('mid'));

        // 是否有回复权限
        $isHasReply = UserModel::isRoles(UserModel::USER_REPLY);
        $this->assign("isHasReply", $isHasReply);
        $this->assign("lastRecord", $lastRecord);
        $this->assign("nextRecord", $nextRecord);
        $this->assign('listcount', $listcount);
        $this->assign('page', $show);      // 赋值分页输出
        $this->assign("listzb", $listzb); //分页内容
        $this->assign("list", $list);
        $this->assign("mid", $this->_get('mid'));
        $this->display();
    }

    public function Mcontent_add()
    {
        $this->display();
    }

    /**
     * 清除缓存
     */
    public function clear()
    {
        $moveId = $this->_get("mId");
        $move = new MoveModel();

        // 清除缓存
        $c = $move->clear($moveId);
        if ($c)
        {
            $this->success("清除缓存成功！");
        }
        else 
        {
            $this->error("清除缓存失败！");
        }
    }

    public function Madd()
    {
        //添加动态
        if ($this->ispost())
        {
            // 验证添加权限
            $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

            if (empty($isHasInsert)) 
            {
                $this->error("您没有添加权限，如有问题，请咨询管理员！");
            }

            $move = D("move");

            $data = array(
                "m_content" => $this->_post('m_content'),
                "m_date" => time(),
                "u_id" => session('u_id'),
                "m_title" => $this->_post('m_title'),
            );

            //加载自动验证.
            if ($move->create())
            {
                if ($move->add($data))
                {
                    $this->success("添加动态成功！", "__URL__/index");
                }
                else
                {
                    $this->error();
                }
            }
            else
            {
                $this->error($move->getError());
            }
        }
    }

    //删除数据
    public function del()
    {
        if ($this->_get('mid')) {
            // 验证删除权限
            $isHasDelete = UserModel::isRoles(UserModel::USER_DELETE);

            if (empty($isHasDelete)) {
                $this->error("您没有删除权限，如有问题，请咨询管理员！");
            }

            $move = M("move");
            //事务开启
            $move->startTrans();

            $arr = $move->where("m_id=" . $this->_get('mid'))->delete();
            //删除留言回复
            M("replay_zb")->where("Link_id=" . $this->_get('mid') . " and zb_type=" . self::$CLASS_MOVE)->delete();

            if ($arr)
            {
                //事物提交.
                $success = $move->commit();

                if ($success) 
                {
                    $this->success("删除成功！");
                }
                else
                {
                    $this->error("删除失败！");
                }
            }
            else
            {
                $move->rollback();
                $this->error("删除失败！");
            }
        }
    }
    public function liuyan(){
        // 验证删除权限
        $isHasReply = UserModel::isRoles(UserModel::USER_REPLY);

        if (empty($isHasReply)) {
            $this->error("您没有回复权限，如有问题，请咨询管理员！");
        }

        $replay_zb = M("replay_zb");

        if ($this->isPost())
        {
            if (!$this->_post('lytext'))
            {
                $this->error("亲，没填写评论就想提交啊!");
            }

            $data = array(
                "zb_content" => $this->_post('lytext'),
                "zb_date" => date("Y-m-d H:i:s"),
                "zb_type" => self::$CLASS_MOVE,
                "u_id" => session('u_id'),
                "Link_id" => $this->_get('mid'),
            );

            $arr = $replay_zb->add($data);

            if ($arr)
            {
                $this->success("评论成功！");
            } else
            {
                $this->error("评论失败！");
            }
        }
    }
}
?>